<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import DateUtil from '@/utils/date'
//引入message的api
import { putClearOneMessage } from '@/apis/message/index'

//定义消息列表数据
interface MessageListData {
    show: boolean
    lastTime: string
    lastMessage: Message
    id: string
    userId: string
    nickname: string
    avatar: string
    handled: boolean
    [key: string]: any
}

//接收父组件传递的参数
const props = defineProps({
    //消息列表
    data: {
        type: Object as () => MessageListData,
        default: () => {
            return {}
        },
    },
    show: {
        type: Boolean,
        default: false,
    },
})
// console.log(props.data.userId, 1111111111111)
// show控制是否显示滑动按钮

onLoad(() => {
    // console.log(props.data)
})

//跳转到消息详情
const handleGoMessageDetail = async () => {
    handleChangeMessageRead()
    uni.navigateTo({
        url: `/basePackage/pages/customerService/CustomerService?useid=${props.data.chatWithUserInfo.userId}&username=${props.data.chatWithUserInfo.nickname}&useravatar=${props.data.chatWithUserInfo.avatar}`,
    })
    console.log('跳转到消息详情')
}
interface Message {
    id: string
    createTime: string
    shopId: string
    adminId: string
    userId: string
    senderType: string
    receiverType: string
    messageType: MessageType
    message: string
    read: boolean
}
enum MessageType {
    //文本
    TEXT = 'TEXT',
    //图片
    IMAGE = 'IMAGE',
    //商品
    PRODUCT = 'PRODUCT',
    //未及时处理的消息
    UN_HANDLE = 'UN_HANDLE',
    //订单
    ORDER = 'ORDER',
}
const renderMessage = (message: Message) => {
    if (!message) return ''
    switch (message.messageType) {
        case MessageType.UN_HANDLE:
            return '[未处理消息]'
        case MessageType.PRODUCT:
            return '[商品]'
        case MessageType.IMAGE:
            return '[图片]'
        case MessageType.ORDER:
            return '[订单]'
        default:
            return message.message
    }
}
/**
 * 单条消息已读
 */
const handleChangeMessageRead = async () => {
    const res = await putClearOneMessage(props.data.chatWithUserInfo.userId)
    console.log(res)
}

const renderTime = (time: any) => {
    if (!time) return ''

    const lastTime = new Date(+time)
    const dateUtil = new DateUtil(new Date(+time))
    const isToday = new Date().getDay() === lastTime.getDay()
    return isToday ? dateUtil.getH() + ':' + dateUtil.getMin() : dateUtil.getYMD()
}
</script>

<template>
    <view @click="handleGoMessageDetail">
        <view class="item u-border-bottom">
            <view class="item__left" mode="aspectFill">
                <u-avatar size="122" shape="round" :src="props.data.chatWithUserInfo.avatar"></u-avatar>
            </view>
            <!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
            <view class="title-wrap">
                <text class="title u-line-2">{{ props.data.title }}</text>
            </view>
            <view class="item__right-wrap">
                <view class="messagetop">
                    <view class="messagetop__name">{{ props.data.chatWithUserInfo.nickname }}</view>
                    <view class="messagetop__time"> {{ renderTime(props.data.lastMessage?.sendTime) }}</view>
                </view>
                <view class="messagebottom">
                    <view v-if="props.data.lastMessage.message" class="main__right--msg">
                        {{ renderMessage(props.data.lastMessage) }}
                    </view>

                    <template v-if="!props.data.unreadNumber"><view>√</view></template>
                    <template v-else
                        ><view class="messagebottom__total">{{ props.data.unreadNumber }}</view></template
                    >
                </view>
            </view>
        </view>
    </view>
</template>

<style lang="scss" scoped>
@include b(item) {
    display: flex;
    padding: 42rpx 32rpx;
    @include e(left) {
        width: 122rpx;
        height: 122rpx;
        border-radius: 148rpx;
        margin-right: 30rpx;
    }
    @include e(right-wrap) {
        width: 526rpx;
        display: flex;
        flex-direction: column;
    }
}

@include b(messagetop) {
    display: flex;
    justify-content: space-between;
    margin-bottom: 18rpx;
    @include e(name) {
        color: #101010;
        font-size: 28rpx;
    }
    @include e(time) {
        color: rgba(153, 153, 153, 1);
        font-size: 24rpx;
    }
}
@include b(messagebottom) {
    display: flex;
    justify-content: space-between;
    align-items: center;
    @include e(text) {
        width: 430rpx;
        height: 62rpx;
        color: rgba(153, 153, 153, 1);
        font-size: 24rpx;
    }
    @include e(total) {
        width: 36rpx;
        height: 36rpx;
        line-height: 36rpx;
        border-radius: 18rpx;
        background-color: rgba(71, 85, 233, 1);
        color: #101010;
        font-size: 24rpx;
        text-align: center;
    }
}
</style>
